
package com.jf.cloud.coupon.listener;

import com.jf.cloud.api.order.constant.OrderStatus;
import com.jf.cloud.common.order.bo.RefundNotifyBO;
import com.jf.cloud.common.rocketmq.config.RocketMqConstant;
import com.jf.cloud.common.util.Json;
import com.jf.cloud.coupon.service.CouponLockService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author zz
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.ORDER_REFUND_SUCCESS_SERVICE_TOPIC,consumerGroup = RocketMqConstant.ORDER_REFUND_SUCCESS_COUPON_GROUP)
public class OrderRefundSuccessCouponConsumer implements RocketMQListener<RefundNotifyBO> {

    private static final Logger LOG = LoggerFactory.getLogger(OrderRefundSuccessCouponConsumer.class);

    @Autowired
    private CouponLockService couponLockService;


    /**
     * 订单退款，还原商家优惠券，平台券无法还原
     * @param message
     */
    @Override
    public void onMessage(RefundNotifyBO message) {
        if (message.getCanRefund() && Objects.equals(message.getOrderStatus(), OrderStatus.CLOSE.value())) {
            LOG.info("订单退款成功，还原商家优惠券... message: " + Json.toJsonString(message));
            couponLockService.reductionCoupon(message.getOrderId());
        }
    }
}
